﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace MLY.Admin
{
    public partial class WebForm1 : Bll.BasePage
    {
        protected int pageNum
        {
            get
            {
                if (Session["pageNum"] == null)
                    return 0;
                return int.Parse(Session["pageNum"].ToString());
            }
            set
            {
                Session["pageNum"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataSource = Bll.clsOrganization.GetOrgByNameAndClassification(0, "");
                GridView1.DataBind();
                Bll.clsOrganization o = new MLY.Bll.clsOrganization();
              
                lblSuccessMsg.Visible = false;
                lblErrMsg.Visible = false;
                lblNoOrgFound.Visible = false;
            }
            else
            {

            }
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            int id = Int32.Parse(((ImageButton)sender).CommandArgument);
            if (Bll.clsOrganization.DeleteOrg(id))
            {
                lblSuccessMsg.Visible = true;
                lblErrMsg.Visible = false;
                BindGrid();
            }
            else
            {
                lblErrMsg.Visible = true;
                lblSuccessMsg.Visible = false;
            }


        }
        protected void imgbtnSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            BindGrid();
        }



        protected void BindGrid()
        {
            GridView1.DataSource = Bll.clsOrganization.GetOrgByNameAndClassification(int.Parse(this.ddlOrgClassification.SelectedValue.ToString()), this.txtOrgName.Text.ToString());
            GridView1.DataBind();
            int rows = GridView1.Rows.Count;
            if (rows == 0)
            {
                lblNoOrgFound.Visible = true;
            }
        }

        protected void ddlOrgClassification_PreRender(object sender, EventArgs e)
        {
            if ((ddlOrgClassification.Items.Count == 0) ||
                (ddlOrgClassification.Items[0].Value != "0"))
            {
                ListItem all = new ListItem("All", "0");
                ddlOrgClassification.Items.Insert(0, all);
            }
        }
        protected void GridView1_PreRender(object sender, EventArgs e)
        {
            if ((GridView1.Rows.Count == 0) && (Session["OrganizationId"] != null))
            {
                divEmptyGrid.Visible = true;
            }
            else
            {
                divEmptyGrid.Visible = false;
            }
        }

        protected void imgAddNewOnNoRows_Click(object sender, ImageClickEventArgs e)
        {
            Response.RedirectTo("editOrg.aspx?Edit=-1");
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataSource = Bll.clsOrganization.GetOrgByNameAndClassification(0, "");
            GridView1.DataBind();
        }
    }
}
